﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Web.View.AdminView
{
    public partial class TireQuotation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Bind car data to gridview
            BindQuotationList(db);

            // Change gridview pagesize based on dropdownlist
            gvCustomerTireQuotationList.SettingsPager.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);

            // Dispose connection to database
            db.Dispose();
        }

        /// <summary>
        /// Function that bind quotations to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        private void BindQuotationList(TireServiceDB db)
        {
            //// Bind to gridview - show all quotations
            //gvCustomerTireQuotationList.DataSource = db.TireQuotationViews.Where(p => p.SendOut == null).ToList();
            //gvCustomerTireQuotationList.DataBind();
        }

        protected void btnMassUpdate_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    TireServiceDB db = new TireServiceDB();

            //    List<TireQuotationView> quotations = db.TireQuotationViews.Where(p => p.SendOut == null).ToList();

            //    foreach (TireQuotationView quotation in quotations)
            //    {
            //        string AcceptLink = "http://www.mobilhjulskift.dk/GodkendTilbud.aspx?action=1&id=" + quotation.QuotationReferenceId.ToString();
            //        string RejectLink = "http://www.mobilhjulskift.dk/GodkendTilbud.aspx?action=0&id=" + quotation.QuotationReferenceId.ToString();

            //        string HF = string.Format("{0:N}", quotation.HF);
            //        string VF = string.Format("{0:N}", quotation.VF);
            //        string HB = string.Format("{0:N}", quotation.HB);
            //        string VB = string.Format("{0:N}", quotation.VB);
            //        string Qty = quotation.Quantity.ToString();
            //        decimal tirepriceexvat = (Convert.ToDecimal(quotation.CostPrice) + CalculateTirePrice(Convert.ToInt32(quotation.TireSize)));
            //        decimal envpriceexvat = 10;
            //        decimal montpriceexvat = CalculateMountingPrice(Convert.ToInt32(quotation.TireSize));
            //        decimal totalexvat = (tirepriceexvat * quotation.Quantity) + (envpriceexvat * quotation.Quantity) + (montpriceexvat * quotation.Quantity);
            //        decimal vat = totalexvat * Convert.ToDecimal(0.25);
            //        decimal total = totalexvat * Convert.ToDecimal(1.25);
            //        string TirePriceExVat = string.Format("{0:N}", tirepriceexvat);
            //        string TirePriceTotalExVat = string.Format("{0:N}", (tirepriceexvat * quotation.Quantity));
            //        string EnvPriceTotalExVat = string.Format("{0:N}", (envpriceexvat * quotation.Quantity));
            //        string MontPriceExVat = string.Format("{0:N}", montpriceexvat);
            //        string MontPriceTotalExVat = string.Format("{0:N}", (montpriceexvat * quotation.Quantity));
            //        string TotalExVat = string.Format("{0:N}", totalexvat);
            //        string TotalVat = string.Format("{0:N}", vat);
            //        string Total = string.Format("{0:N}", total);

            //        string Note = "";

            //        if (quotation.Note != string.Empty & quotation.Note != "" & quotation.Note != null)
            //        {
            //            Note = quotation.Note + "<br><br>";
            //        }

            //        // Send email to customer
            //        EmailNotificationServices emailBLL = new EmailNotificationServices();
            //        emailBLL.SendTireQuotation(quotation.Email, quotation.CustomerId, quotation.Name, quotation.RegistrationNumber, Note, HF, VF, HB, VB, AcceptLink, RejectLink, quotation.Description, Qty, TirePriceExVat, TirePriceTotalExVat, EnvPriceTotalExVat, MontPriceExVat, MontPriceTotalExVat, TotalExVat, TotalVat, Total);

            //        // Get quotation and update
            //        CustomerTireSetQuotationList editquotation = db.CustomerTireSetQuotationLists.Single(p => p.QuotationReferenceId == quotation.QuotationReferenceId);
            //        editquotation.SendOut = DateTime.Now;

            //        db.SubmitChanges();
            //    }

            //    lMessage.Text = "Opdatering gennemført";

            //    db.Dispose();

            //}
            //catch (Exception ex)
            //{
            //    string error = ex.Message;

            //    lMessage.Text = error;
            //}
        }

        private decimal CalculateTirePrice(int TireSize) 
        {
            decimal price = 125;

            if (TireSize < 16)
            {
                price = 100;
            }

            if (TireSize > 15 & TireSize < 18)
            {
                price = 125;
            }

            if (TireSize > 17)
	        {
                price = 150;
	        }

            return price;
        }

        private decimal CalculateMountingPrice(int TireSize)
        {
            decimal price = 135;

            if (TireSize > 17)
            {
                price = 180;
            }

            return price;
        }

    }
}